package com.sais.manage.boot.platform.controller;

import com.sais.common.entity.Result;
import com.sais.manage.boot.platform.annotation.Throttle;
import com.sais.manage.boot.platform.dto.DictItemDTO;
import com.sais.manage.boot.platform.dto.DictItemSaveDTO;
import com.sais.manage.boot.platform.service.IDictItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 宿舍管理系统-字典项 controller 接口
 *
 * @author zhang
 * @date 2019/12/2
 **/
@Api(tags = "字典项管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/platform/dict/item")
public class DictItemController {

    @Resource
    private IDictItemService dictItemService;

    @ApiOperation(value = "保存 字典项")
    //@Idempotent
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Result saveDictItem(@RequestBody DictItemSaveDTO dictItemSaveDTO) {
        dictItemService.saveDictItem(dictItemSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "新建 字典项")
    // @Idempotent
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public Result createDictItem(@RequestBody DictItemSaveDTO dictItemSaveDTO) {
        dictItemService.createDictItem(dictItemSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "删除 字典项")
    @Throttle
    @RequestMapping(value = "delete/{dictItemId}", method = RequestMethod.GET)
    public Result dictItemService(@PathVariable(value = "dictItemId") String dictItemId) {
        dictItemService.deleteDictItem(dictItemId);
        return Result.response();
    }

    @ApiOperation(value = "更新 字典项")
    @Throttle
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result updateDictItem(@RequestBody DictItemSaveDTO dictItemSaveDTO) {
        dictItemService.updateDictItem(dictItemSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "根据 字典id 查询 字典项")
    @Throttle
    @RequestMapping(value = "list/{dictId}", method = RequestMethod.GET)
    public Result listDictItemByDictId(@PathVariable(value = "dictId") String dictId) {
        List<DictItemDTO> dictItems = dictItemService.listDictItemByDictId(dictId);
        return Result.response().data("dictItems", dictItems);
    }

    @ApiOperation(value = "根据 字典code 查询 字典项")
    @Throttle
    @RequestMapping(value = "list/code/{dictCode}", method = RequestMethod.GET)
    public Result listDictItemByDictCode(@PathVariable(value = "dictCode") String dictCode) {
        List<DictItemDTO> dictItems = dictItemService.listDictItemByDictCode(dictCode);
        return Result.response().data("dictItems", dictItems);
    }

}

